package com.school.controller.admin;

import com.school.common.ServiceResultEnum;
import com.school.entity.AdminUser;

import com.school.service.AdminUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * ClassName: AdminController
 * Package: com.school.controller.admin
 * Description: 管理员控制器
 * @Author Laohe
 * @Create 2025-07-09 10:55
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminUserService adminUserService;

    @GetMapping("/login")
    public String loginPage() {
        return "admin/login";
    }

    @GetMapping("/index")
    public String index(HttpServletRequest request) {
        // 设置请求属性 "path" 为 "index"
        request.setAttribute("path", "index");
        // 返回视图名称 "admin/index"
        return "admin/index";
    }


    @PostMapping("/login")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        @RequestParam("verifyCode") String verifyCode,
                        HttpSession session) {
        if (StringUtils.isEmpty(verifyCode)) {
            session.setAttribute("errorMsg", "验证码不能为空");
            return "admin/login";
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "admin/login";
        }
        String kaptcha = session.getAttribute("verifyCode") + "";
        if (!kaptcha.equalsIgnoreCase(verifyCode)) {
            session.setAttribute("errorMsg", "验证码错误");
            return "admin/login";
        }

        AdminUser adminUser = adminUserService.login(userName, password);
        if (adminUser != null) {
            session.setAttribute("loginUser", adminUser.getNickName());
            session.setAttribute("loginUserId", adminUser.getAdminUserId());
            return "redirect:/admin/index";
        } else {
            session.setAttribute("errorMsg", "登陆失败");
            return "admin/login";
        }
    }

    @GetMapping("/logout")
    public String loginout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUser");
        request.getSession().removeAttribute("loginUserId");
        request.getSession().removeAttribute("errorMsg");
        return "admin/login";
    }

    @GetMapping("/profile")
    public String profile(HttpServletRequest request) {
        Integer UserId = (int) request.getSession().getAttribute("loginUserId");
        AdminUser adminUser = adminUserService.getUserDetailById(UserId);
        if (adminUser == null) {
            return "admin/login";
        }
        request.setAttribute("path", "profile");
        request.setAttribute("loginUser", adminUser.getLoginUserName());
        request.setAttribute("nickName", adminUser.getNickName());
        return "admin/profile";
    }
    @PostMapping("/profile/name")
    @ResponseBody
    public String nameUpdate(HttpServletRequest request,
                             @RequestParam("loginUserName") String loginUserName,
                             @RequestParam("nickName") String nickName) {

        if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)) {
            return "用户名或昵称不能为空";
        }
        Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
        int count = adminUserService.updateName(loginUserId, loginUserName, nickName);
        if (count >0) {

            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            return ServiceResultEnum.ERROR.getResult();
        }
    }


}